package com.wye.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wye.dao.CategoryMapper;
import com.wye.dao.ParaMapper;
import com.wye.goods.pojo.Category;
import com.wye.goods.pojo.Para;
import com.wye.service.ParaService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ParaServiceImpl implements ParaService {

    private static Logger logger = LoggerFactory.getLogger(ParaServiceImpl.class);

    @Autowired
    private ParaMapper paraMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    public List<Para> findByCategory(Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        return paraMapper.findByCategory(category.getTemplateId());
    }

    @Override
    public List<Para> findAll() {
        return paraMapper.selectAll();
    }

    @Override
    public Para findById(Integer id) {
        if (id != null){
            return paraMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public int insert(Para Para) {
        return paraMapper.insertSelective(Para);
    }

    @Override
    public void update(Para Para) {
        paraMapper.updateByPrimaryKeySelective(Para);
    }

    @Override
    public void delete(Integer id) {
        paraMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Para> findList(Para para) {
        return null;
    }

//    @Override
//    public List<Para> findList(Para Para) {
//        List<Para> ParaList = new ArrayList<>();
//        if (Para != null){
//            if (!StringUtils.isEmpty(Para.getName())){
//                String name = "%" + Para.getName() + "%";
//                logger.info("name条件={}", name);
//                ParaList = paraMapper.selectByLikeName(name);
//            }
//            if (!StringUtils.isEmpty(Para.getLetter())){
//                ParaList = paraMapper.selectByLikeLetter("%" + Para.getLetter() + "%");
//            }
//            if (!StringUtils.isEmpty(Para.getName()) && !StringUtils.isEmpty(Para.getLetter())){
//                ParaList = paraMapper.selectByLikeLetterOrName(Para.getLetter(), Para.getName());
//            }
//
//        }
//        return ParaList;
//
//    }

    @Override
    public PageInfo<Para> findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Para> ParaList = paraMapper.selectAll();
        //封装PageInfo
        return new PageInfo<>(ParaList);
    }




}
